package com.lottery.im.module.result.ui;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.api.result.entity.AllGameInfo;
import com.lottery.im.api.result.entity.AllGameSelectInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.result.adapter.AllGameSelectAdapter;
import com.lottery.im.module.result.presenter.AllGamePresenter;
import com.lottery.im.module.result.view.AllGameView;
import java.util.ArrayList;
import java.util.List;

/**
 * 所有彩种筛选
 */
public class AllGameSelectFragment extends BaseSwipeFragment<AllGamePresenter>
    implements AllGameView {

  @BindView(R.id.game_recycler) RecyclerView mRecyclerView;
  private List<AllGameSelectInfo> mAllGameList = new ArrayList<>();
  private AllGameSelectAdapter mAdapter;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_all_game_select;
  }

  @Override protected void initPresenter() {
    presenter = new AllGamePresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mAdapter = new AllGameSelectAdapter(mAllGameList);
    mRecyclerView.setAdapter(mAdapter);
    mAdapter.setOnGameClickListener((position, childPosition) -> {
      mAllGameList.get(position)
          .getChildGameTypeList()
          .get(childPosition)
          .setSelect(
              !mAllGameList.get(position).getChildGameTypeList().get(childPosition).isSelect());
      mAdapter.notifyDataSetChanged();
    });
    mAdapter.setOnCheckAllClickListener(position -> {
      mAllGameList.get(position).setCheckAll(!mAllGameList.get(position).isCheckAll());
      List<AllGameSelectInfo.ChildGameTypeListBean> childGameTypeList =
          mAllGameList.get(position).getChildGameTypeList();
      for (int i = 0; i < childGameTypeList.size(); i++) {
        childGameTypeList.get(i).setSelect(mAllGameList.get(position).isCheckAll());
      }
      mAdapter.notifyDataSetChanged();
    });
    presenter.queryAllGame();
  }

  @OnClick({ R.id.iv_back, R.id.tv_finish })
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.tv_finish:
        ArrayList<String> mGameTypeList = new ArrayList<>();
        for (int i = 0; i < mAllGameList.size(); i++) {
          List<AllGameSelectInfo.ChildGameTypeListBean> childGameTypeList =
              mAllGameList.get(i).getChildGameTypeList();
          for (int j = 0; j < childGameTypeList.size(); j++) {
            if (childGameTypeList.get(j).isSelect()) {
              mGameTypeList.add(childGameTypeList.get(j).getGameType());
            }
          }
        }
        Intent intent = new Intent();
        intent.putStringArrayListExtra("gameTypeList", mGameTypeList);
        setFragmentResult(Activity.RESULT_OK,intent);
        popBackStack();
        break;
    }
  }

  @Override public void querySuccess(List<AllGameInfo> list) {
    mAllGameList.clear();
    for (int i = 0; i < list.size(); i++) {
      AllGameInfo allGameInfo = list.get(i);
      String mode = allGameInfo.getGameTypeMode();
      String modeName = allGameInfo.getGameTypeModeName();
      List<AllGameInfo.ChildGameTypeListBean> childGameTypeList =
          allGameInfo.getChildGameTypeList();
      List<AllGameSelectInfo.ChildGameTypeListBean> gameTypeList = new ArrayList<>();
      for (int j = 0; j < childGameTypeList.size(); j++) {
        gameTypeList.add(
            new AllGameSelectInfo.ChildGameTypeListBean(childGameTypeList.get(j).getGameType(),
                childGameTypeList.get(j).getGameTypeName(), false));
      }
      mAllGameList.add(new AllGameSelectInfo(mode, modeName, false, gameTypeList));
    }
    mAdapter.notifyDataSetChanged();
  }
}
